<%@page import="com.feib.soeasy.util.CodeTableUtil"%>
<%@page import="com.feib.soeasy.model.UploadFile"%>
<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<c:choose>
	<c:when test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<c:set var="lastPageNumber" value="${actionBean.queryResult.totalPageCount}"/>
		<c:set var="currentPageNumber" value="${actionBean.queryResult.pageNumber}"/>
		<c:set var="prePageNumber" value="${actionBean.queryResult.pageNumber - 1}"/>
		<c:set var="nextPageNumber" value="${actionBean.queryResult.pageNumber + 1}"/>
		
		<div id="paginationBar">
			<input type="button" id="firstPageBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			<input type="button" id="prePageBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
			第 <input type="text" id="currentPageText" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
			<input type="button" id="nextPageBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
			<input type="button" id="lastPageBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
		</div>
		
		<table id="listTable">
		<thead>
			<tr>
				<th>上傳日期</th>
				<th>檔案名稱</th>
				<th>上傳格式</th>
				<th>上傳人員</th>
				<th>總筆數</th>
				<th>總金額</th>
				<th>放行人員</th>
				<%if ( UserUtil.isBankGroup()){ %>
				<th>業者名稱</th>
				<%} %>
				<th>上傳結果</th>
				<th>&nbsp;</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
			<tr>
				<td align="center"><fmt:formatDate value="${viewObj.uploadDate}" pattern="yyyy/MM/dd" /><br/>
					<fmt:formatDate value="${viewObj.uploadDate}" pattern="HH:mm:ss" />
				</td>
				<td><c:out value="${viewObj.fileName}"/></td>
				<td><c:out value="${viewObj.formatDefinition.formatName}"/></td>
				
				<td>
					<c:choose>
						<c:when test="${null != viewObj.uploadUser }"><c:out value="${viewObj.uploadUser.userName}"/></c:when>
						<c:otherwise>&nbsp;</c:otherwise>
					</c:choose>
				</td> 
				<td align="right"><fmt:formatNumber value="${viewObj.totalItems}" pattern="##,###" /></td>
				<td align="right"><fmt:formatNumber value="${viewObj.totalAmount}" pattern="##,###" /></td>
				<td>
					<c:choose>
						<c:when test="${null != viewObj.confirmUser }"><c:out value="${viewObj.confirmUser.userName}"/></c:when>
						<c:otherwise>&nbsp;</c:otherwise>
					</c:choose>
				</td>
				<%if ( UserUtil.isBankGroup()){ %>
				<td><c:out value="${viewObj.group.groupName}" /></td>
				<%} %>
				<td>
				<%
				UploadFile viewObj = (UploadFile)pageContext.getAttribute("viewObj");
				
				if (null == viewObj.getFileStatus())
				{
					out.print("&nbsp;");
				}
				else						
				{
					out.print(CodeTableUtil.getInstance().getCodeName(UploadFile.FILE_STATUS_CODE_KIND, viewObj.getFileStatus().toString()));
				}
				%>
				</td>
				<td>
				<c:choose>
					<c:when test="${2 == viewObj.fileStatus }">
						<div id="viewFileErrorsImg-<c:out value='${viewObj.uploadFilePk}'/>"><a href="javascript: void(0)" class="btnZoom">查詢</a></div>
					</c:when>
					<c:when test="${3 == viewObj.fileStatus || 4 == viewObj.fileStatus}">						
						<c:choose>
					        <c:when test="${1 == viewObj.formatDefinition.formatType }">
						       <div id="veiwFilePaymentsImg-<c:out value='${viewObj.uploadFilePk}'/>"><a href="javascript: void(0)" class="btnZoom">查詢</a></div>
					        </c:when>
					        <c:otherwise>
                                <div id="veiwFilePaysImg-<c:out value='${viewObj.uploadFilePk}'/>"><a href="javascript: void(0)" class="btnZoom">查詢</a></div> 
					        </c:otherwise>
				        </c:choose>
					</c:when>
					<c:otherwise>&nbsp;</c:otherwise>
				</c:choose>
				</td>					
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<div id="paginationBar">
			
			<input type="button" id="printBtn2" value="列印" />		</div>
			
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			
			$('#printBtn2').click(function() {
				//window.print();
				//doQueryPrintAction();
				divPrint('queryResultDiv');//只印目前這筆
			});
			
			
			$("[id^='firstPageBtn']").click(function() {
				doQueryAction(1);
			});
			
			$("[id^='prePageBtn']").click(function() {
				doQueryAction(<c:out value='${prePageNumber}'/>);
			});
			
			$("[id^='nextPageBtn']").click(function() {
				doQueryAction(<c:out value='${nextPageNumber}'/>);
			});
			
			$("[id^='lastPageBtn']").click(function() {
				doQueryAction(<c:out value='${lastPageNumber}'/>);
			});
			
			$("[id^='currentPageText']").change(function() {
				var value = this.value;
				if (value != ''){
					value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
					var intRegex = /^\d+$/;
					if(!intRegex.test(value)) {
						alert('頁數必須是整數');
						return ;
					}
					
					value = parseInt(value, 10);
					
					if (value < 1){
						alert('頁數必須是大於1');
						return ;
					}
					else if (value > <c:out value='${lastPageNumber}'/>){
						alert('頁數必須是小於總頁數 (<c:out value='${lastPageNumber}'/>)');
						return ;
					}
					else if (value != <c:out value='${currentPageNumber}'/>){
						doQueryAction(value);
					}
					
				}
				else{
					alert('頁數不可為空白');
					return ;
				}
			});
			
			$("[id^='viewFileErrorsImg-']").click(function() {
				var filePk = this.id.substring(18);
				doQueryFileErrorReasonAction(1, filePk);
			});
			
			$("[id^='veiwFilePaymentsImg-']").click(function() {
				
				
				var filePk = this.id.substring(20);
				doQueryFilePaymentsAction(1, filePk);
			});
			
			$("[id^='veiwFilePaysImg-']").click(function() {
				
				
				var filePk = this.id.substring(16);
				doQueryFilePayerAction(1, filePk);
			});
			
			
			
			
		});
		//-->
		</script>
	</c:when>
	<c:otherwise>
		<div id="queryNothingBlockDiv" style="display: none;">
			查無資料
		</div>		
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$( "#queryNothingBlockDiv" ).dialog({  title:'帳單上傳記錄查詢' ,width: 300, resizable: false, modal: true, buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}  });
		});
		//-->
		</script>
	</c:otherwise>
</c:choose>